package com.example.rumen;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.util.Objects;

public class Sign_in extends AppCompatActivity {

    private Button btn_signUp;
    private String username_sign;
    private String password_sign;
    private Button btn_login;
    private EditText username_txt;
    private EditText password_txt;
    private String password;
    private String user;

    @SuppressLint({"MissingInflatedId", "WrongViewCast"})
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_sign_in);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        username_sign = getIntent().getStringExtra("user");
        password_sign = getIntent().getStringExtra("pass");

        btn_signUp = findViewById(R.id.button_signup2);
        username_txt = findViewById(R.id.editText_user2);
        password_txt = findViewById(R.id.editText_password);

        btn_signUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), Sign_up.class);
                startActivity(intent);
            }
        });
        //登录

        btn_login = findViewById(R.id.button_login2);
        btn_login.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View v) {
                user = username_txt.getText().toString();
                password = password_txt.getText().toString();
                if (Objects.equals(username_sign, user) && Objects.equals(password_sign, password)) {
                    Toast.makeText(getApplicationContext(), "成功登录", Toast.LENGTH_SHORT).show();
                    //登录成功后跳转到Btns的页面
                    Intent intent = new Intent((getApplicationContext()), Btns.class);
                    password_txt.setText("");
                    username_txt.setText("");
                    password_txt.requestFocus();
                    password_txt.requestFocus();
                    startActivity(intent);
                }


                if (!Objects.equals(username_sign, user) && Objects.equals(password_sign, password)) {
                    Toast.makeText(getApplicationContext(), "用户名错误", Toast.LENGTH_SHORT).show();
                    username_txt.setText("");
                    username_txt.requestFocus();
                }


                if (Objects.equals(username_sign, user) && !Objects.equals(password_sign, password)) {
                    Toast.makeText(getApplicationContext(), "密码错误", Toast.LENGTH_SHORT).show();
                    password_txt.setText("");
                    password_txt.requestFocus();
                }


                if (!Objects.equals(username_sign, user) && !Objects.equals(password_sign, password)) {
                    Toast.makeText(getApplicationContext(), "账号和密码都错误", Toast.LENGTH_SHORT).show();
                    password_txt.setText("");
                    username_txt.setText("");
                    password_txt.requestFocus();
                    password_txt.requestFocus();
                }


            }
        });
    }
}